﻿//查询绑定编辑
function doLoad() {
	var queryObject = new Sys.Data.AdoNetQueryBuilder("Customers");
	var id = Sys.get("#CustomerID").value;
	queryObject.set_filter("CustomerID eq '" + id + "'");
	var dv = Sys.get("$Editor");
	var command = queryObject.toString();
	dv.set_fetchOperation(command);
	dv.fetchData();
}
//提交变化
function doCommit() {
	var pendingChanges = dataContext.get_hasChanges();
	if (pendingChanges !== true) {
		alert("No pending changes to save.");
		return;
	}

	var changes = dataContext.get_changes();
	var buffer = "";
	for (var i = 0; i < changes.length; i++) {
		ch = changes[i];
		buffer += makeReadable(ch.action) + " --> " + ch.item["CustomerID"];
		buffer += "\n\tNew address: " + ch.item["Address"];
		buffer += "\n";
	}

	if (confirm(buffer))
		dataContext.saveChanges();
}

var dataContext;
var queryObject, queryObject1;

Sys.require([Sys.components.dataView, Sys.components.adoNetDataContext]);
//DOM加载完成后执行
Sys.onReady(function () {
	dataContext = Sys.create.adoNetDataContext(
        {
        	serviceUri: "/Services/NorthwindService.svc",
        	mergeOption: Sys.Data.MergeOption.appendOnly
        });
	dataContext.initialize();

	queryObject = new Sys.Data.AdoNetQueryBuilder("Customers");
	queryObject.set_orderby("ContactName");            //$orderby
	queryObject.set_filter("City eq " + "'London'");   // $filter
	queryObject.set_expand("Orders");                  //$expand

	queryObject1 = new Sys.Data.AdoNetQueryBuilder("Customers");
	queryObject1.set_filter("startswith(CompanyName, 'A')");
});

function makeReadable(action) {
	if (action === 0)
		return "Insert";
	if (action === 1)
		return "Update";
	if (action === 2)
		return "Delete";
}
